<template>
  <el-dialog
    :title="!houses.sid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="地铁id" prop="mid">
      <el-input v-model="houses.mid" placeholder="地铁id"></el-input>
    </el-form-item>
    <el-form-item label="站点" prop="station">
      <el-input v-model="houses.station" placeholder="站点"></el-input>
    </el-form-item>
    <el-form-item label="站点地址" prop="addr">
      <el-input v-model="houses.addr" placeholder="站点地址"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="houses.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
      <el-input v-model="houses.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="houses.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        houses: {
          sid: 0,
          mid: '',
          station: '',
          addr: '',
          longitude: '',
          latitude: '',
          sort: ''
        },
        dataRule: {
          mid: [
            { required: true, message: '地铁id不能为空', trigger: 'blur' }
          ],
          station: [
            { required: true, message: '站点不能为空', trigger: 'blur' }
          ],
          addr: [
            { required: true, message: '站点地址不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.sid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.sid) {
            this.$http({
              url: this.$http.adornUrl(`/area/subwaystation/info/${this.houses.sid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.mid = data.subwayStation.mid
                this.houses.station = data.subwayStation.station
                this.houses.addr = data.subwayStation.addr
                this.houses.longitude = data.subwayStation.longitude
                this.houses.latitude = data.subwayStation.latitude
                this.houses.sort = data.subwayStation.sort
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/area/subwaystation/${!this.houses.sid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sid': this.houses.sid || undefined,
                'mid': this.houses.mid,
                'station': this.houses.station,
                'addr': this.houses.addr,
                'longitude': this.houses.longitude,
                'latitude': this.houses.latitude,
                'sort': this.houses.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
